package com.ruoyi.lyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.lyy.RemoteLyyService;
import com.ruoyi.lyy.domain.pojo.Merchant;
import com.ruoyi.lyy.domain.pojo.Order;
import com.ruoyi.lyy.domain.pojo.User;
import com.ruoyi.lyy.domain.vo.DashboardRowOneVO;
import com.ruoyi.lyy.domain.vo.DashboardRowTwoVO;
import com.ruoyi.lyy.domain.vo.RowTwoOrderCount;
import com.ruoyi.lyy.mapper.MerchantMapper;
import com.ruoyi.lyy.mapper.OrderMapper;
import com.ruoyi.lyy.mapper.UserMapper;
import com.ruoyi.lyy.service.IDashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YueYang
 * Created on 2025/11/13 16:19
 * @version 1.0
 * 控制台接口 pc端首页 服务层 实现类
 */
@Service
@RequiredArgsConstructor
public class DashboardServiceImpl implements IDashboardService {

    //注入orderMapper
    private final OrderMapper orderMapper;

    //注入userMapper
    private final UserMapper userMapper;

    //注入merchantMapper
    private final MerchantMapper merchantMapper;

    //注入remoteLyyService
    private final RemoteLyyService remoteLyyService;


    /**
     * 获取首页第一行数据，根据开始时间和结束时间，默认是当前时间
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 首页数据
     */
    @Override
    public AjaxResult getRowOne(String startTime, String endTime) {
        //ruoyi-api-yueyang中feign提供的接口 获取首页第一行数据
        //这里我是自己调用自己
        return remoteLyyService.getRowOneByFeign(startTime, endTime);

    }

    /**
     * 实际的获取首页第一行数据的方法，根据开始时间和结束时间，默认是当前时间
     * openFeign调用这个接口拿到首页第一行数据
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 首页数据
     */
    @Override
    public DashboardRowOneVO getRealRowOne(String startTime, String endTime) {
        //1.创建封装RowOne返回vo对象
        DashboardRowOneVO dashboardRowOneVO = new DashboardRowOneVO();

        //2. 查询订单表，根据开始时间和结束时间 统计指定范围的订单总额和数量
        //2.1查询指定范围内的订单总额
        Double orderTotalMoney = orderMapper.getOrderTotal(startTime, endTime);
        //2.2 统计指定范围内的订单数量
        //构建查询条件
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<Order>();
        queryWrapper.between(Order::getOrderTime, startTime, endTime);
        //执行查询
        Long orderTotalCount = orderMapper.selectCount(queryWrapper);

        //2.3 封装查询结果
        dashboardRowOneVO.setOrderTotal(orderTotalMoney);
        dashboardRowOneVO.setOrderCount(orderTotalCount);


        //3. 查询用户表获取指定范围新增用户数量
        //3.1 构建查询条件
        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<User>();
        userQueryWrapper.between(User::getRegTime, startTime, endTime);
        //3.2执行查询
        Long userTotalCount = userMapper.selectCount(userQueryWrapper);
        //3.3 封装查询结果
        dashboardRowOneVO.setNewUserCount(userTotalCount);

        //4. 查询商户表 统计指定范围新增商户数量
        //4.1 构建查询条件
        LambdaQueryWrapper<Merchant> merchantQueryWrapper = new LambdaQueryWrapper<Merchant>();
        merchantQueryWrapper.between(Merchant::getRegTime, startTime, endTime);
        //4.2执行查询
        Long merchantTotalCount = merchantMapper.selectCount(merchantQueryWrapper);
        //4.3 封装查询结果
        dashboardRowOneVO.setNewMerchantCount(merchantTotalCount);


        return dashboardRowOneVO;
    }

    /**
     * 获取首页第二行数据，根据开始时间和结束时间，默认是当前时间
     * 订单统计； 救援任务统计；用户概况
     *
     * @return 首页数据
     */
    @Override
    public DashboardRowTwoVO getRowTwoData() {

        //1.创建封装RowTwo返回vo对象
        DashboardRowTwoVO dashboardRowTwoVO = new DashboardRowTwoVO();


        //2. 订单统计部分 查询订单表根据service_type_id进行分类 获取维修，洗车等的统计数量
        List<RowTwoOrderCount> orderCountList = orderMapper.getOrderCountList();
        //非空判断
        if (orderCountList == null) {
            orderCountList = new ArrayList<>();
        }

        //3. 救援任务统计 查询救援任务表 根据status字段进行分类 3是待处理 4是已完成
        List<RowTwoOrderCount> rescueTaskCountList = orderMapper.getRescueTaskCountList();
        //非空判断
        if (rescueTaskCountList == null) {
            rescueTaskCountList = new ArrayList<>();
        }

        //4. 用户概况 查询商户表 获取总的商户；查询用户表获总的用户
        //4.1 查询商户表 获取总的商户
        Long merchantTotalCount = merchantMapper.selectCount(new QueryWrapper<Merchant>());
        if (merchantTotalCount == null) {
            merchantTotalCount = 0L;
        }

        //4.2 查询用户表获总的用户
        Long userTotalCount = userMapper.selectCount(new LambdaQueryWrapper<User>());
        if (userTotalCount == null) {
            userTotalCount = 0L;
        }

        //5. 封装查询结果
        dashboardRowTwoVO.setMerchantTotalCount(merchantTotalCount);
        dashboardRowTwoVO.setUserTotalCount(userTotalCount);
        dashboardRowTwoVO.setOrderCountList(orderCountList);
        dashboardRowTwoVO.setRescueTaskCountList(rescueTaskCountList);

        return dashboardRowTwoVO;

    }

    /**
     * 获取首页第三行数据，柱状图返回name value格式的数据
     *
     * @return 首页数据
     */
    @Override
    public List<RowTwoOrderCount> getRowThreeData() {
        List<RowTwoOrderCount> userOrderCountList = orderMapper.getUserOrderCountList();
        if (userOrderCountList == null) {
            userOrderCountList = new ArrayList<>();
        }
        return userOrderCountList;
    }


}
